package com.imooc.controller;

import com.imooc.domain.Girl;
import com.imooc.properties.GirlProperties;
import com.imooc.repository.GirlRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by Administrator on 2017/7/13.
 */
@RestController
public class TestController {
//
//    @Value("${cubSize}")
//    private String cupSize;
//
//    @Value("${age}")
//    private Integer age;

    @Autowired
    private GirlProperties girlProperties;

    @Autowired
    private GirlRepository girlRepository;
//
//    @Value("${content}")
//    private String content;

//    @RequestMapping(value = {"/hello", "/hi"}, method = RequestMethod.GET)
    @GetMapping({"/hello", "/hi"})
    public String helloView(
            @RequestParam(value = "id", required = false, defaultValue = "0")Integer id) {
        return girlProperties.toString() + "---" + id;
    }

    @GetMapping("/getAll")
    public List<Girl> getAllGirl() {
        return girlRepository.findAll();
    }
    @GetMapping("/girls")
    public Girl getAllGirl(
            @Valid Girl girl, BindingResult bindingResult) {
        //检查是否有错误信息
        if (bindingResult.hasErrors()) {
            System.out.println(bindingResult.getFieldError().getDefaultMessage());
            return null;
        }
        return girl;
    }


    //查询一个女生
    @GetMapping(value = "/girls/{id}")
    public Girl girlFindOne(@PathVariable("id") Integer id) {
        return girlRepository.findOne(id);
    }
}
